package com.zd.bms.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zd.bms.bean.vo.req.HardwareInfoListReqVo;
import com.zd.bms.service.IHardwareInfoService;
import com.zd.bms.service.IScenicSpotService;
import com.zd.common.bean.model.BmsUser;
import com.zd.common.bean.model.HardwareInfo;
import com.zd.common.bean.model.ScenicSpot;
import com.zd.common.common.utils.StringUtils;
import com.zd.common.dao.common.Page;

/**
 * 景区设备管理
 * 
 * @author lishi
 * @date 2018年11月22日 上午10:22:13
 */
@Controller
@RequestMapping("hardwareInfo")
public class HardwardInfoController extends BaseController {
	@Autowired
	private IHardwareInfoService hardwareInfoService;
	
	@Autowired
	private IScenicSpotService scenicSpotService;
	
	@RequestMapping("index")
	public ModelAndView index() throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hardwareInfo/index");
		return mv;
	}

	@RequestMapping("saveOrUpdateIndex")
	public ModelAndView saveOrUpdateIndex(Integer hardwareInfoId) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("hardwareInfo/saveOrUpdateIndex");
		//校验数据是否合法
		BmsUser bmsUser = getSessionUser();
		Integer scenicSpotId = bmsUser.getScenicSpotId();

		if (hardwareInfoId != null) {
			HardwareInfo hardwareInfo = hardwareInfoService.get(hardwareInfoId);
			if (scenicSpotId != null && scenicSpotId != 0) {
				if (scenicSpotId.equals(hardwareInfo.getScenicSpotId())) {
					//TODO 跳转到没有权限页面
				}
			}
			mv.addObject("hardwareInfo", hardwareInfo);
		}
		mv.addObject("scenicSpotId", scenicSpotId);
		return mv;
	}

	@ResponseBody
	@RequestMapping("list")
	public Map<String, Object> list(HardwareInfoListReqVo reqVo) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		Integer scenicSpotId = bmsUser.getScenicSpotId();
		if (scenicSpotId != null && scenicSpotId != 0) {
			reqVo.setScenicSpotId(scenicSpotId);
		}
		Page<HardwareInfo> hardwareInfoPage = hardwareInfoService.pageHardwareInfo(reqVo);

		List<HardwareInfo> voList = hardwareInfoPage.getList();
		resultMap.put("data", voList);
		resultMap.put("draw", reqVo.getDraw());
		resultMap.put("recordsTotal", voList.size());
		resultMap.put("recordsFiltered", hardwareInfoPage.getTotalRow());
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("saveOrUpdate")
	public Map<String, Object> saveOrUpdate(HardwareInfo hardwareInfo) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		Integer scenicSpotId = bmsUser.getScenicSpotId();
		if (scenicSpotId != null && scenicSpotId != 0 && scenicSpotId != hardwareInfo.getScenicSpotId()) {
			resultMap.put("result", false);
			resultMap.put("errorMsg", "不能操作其它景区的门票!");
			return resultMap;
		}
		HardwareInfo originHardwareInfo = null;
		if (hardwareInfo.getId() != null) {
			originHardwareInfo = hardwareInfoService.get(hardwareInfo.getId());
			if (originHardwareInfo.getScenicSpotId() != hardwareInfo.getScenicSpotId()) {
				resultMap.put("result", false);
				resultMap.put("errorMsg", "非法数据!");
				return resultMap;
			}
			originHardwareInfo.setName(hardwareInfo.getName());
			originHardwareInfo.setUpdateTime(new Date());
			hardwareInfoService.update(originHardwareInfo);
		} else {
			ScenicSpot scenicSpot = scenicSpotService.get(scenicSpotId);
			originHardwareInfo = new HardwareInfo();
			if(scenicSpot != null) {
				originHardwareInfo.setScenicSpotId(scenicSpotId);
				originHardwareInfo.setAppId(scenicSpot.getAppId());
			}
			originHardwareInfo.setDeviceNo(StringUtils.getRandomWithTimestamp(20));
			originHardwareInfo.setQrDeviceNo(StringUtils.getRandomWithTimestamp(20));
			originHardwareInfo.setIdDeviceNo(StringUtils.getRandomWithTimestamp(20));
			originHardwareInfo.setName(hardwareInfo.getName());
			originHardwareInfo.setCreateTime(new Date());
			originHardwareInfo.setUpdateTime(new Date());
			hardwareInfoService.insert(originHardwareInfo);
		}
		resultMap.put("result", true);
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("changeStatus")
	public Map<String, Object> changeStatus(Integer hardwareInfoId, int status) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		Integer scenicSpotId = bmsUser.getScenicSpotId();
		if (hardwareInfoId != null && hardwareInfoId != 0) {
			HardwareInfo hardwareInfo = hardwareInfoService.get(hardwareInfoId);
			if (hardwareInfo != null) {
				if (scenicSpotId != null && scenicSpotId != 0 && scenicSpotId != hardwareInfo.getScenicSpotId()) {
					resultMap.put("result", false);
					resultMap.put("errorMsg", "不能操作其它景区的设备!");
					return resultMap;
				}
				hardwareInfo.setStatus(status);
				hardwareInfo.setUpdateTime(new Date());
				hardwareInfoService.update(hardwareInfo);
				resultMap.put("result", true);
				return resultMap;
			}
		}
		resultMap.put("result", false);
		resultMap.put("errorMsg", "设备不存在!");
		return resultMap;
	}
}
